//
//  DLTools.m
//  Otago
//
//  Created by 空转风 on 2023/10/26.
//

#import "DLTools.h"

@implementation DLTools
+(void)NeedResetNoViewWithView:(UIView*)tab andArr:(NSArray*)arr{
    if (arr.count>0) {
        [tab dismissNoView];
    }else{
        [tab dismissNoView];
        [tab showNoView:@"Empty" image:ImageMake(@"nodata") certer:CGPointZero];
    }
}
+(void)NeedResetNoViewWithView:(UIView*)tab andArr:(NSArray*)arr point:(CGPoint)poi{
    if (arr.count>0) {
        [tab dismissNoView];
    }else{
        [tab dismissNoView];
        [tab showNoView:@"Empty" image:ImageMake(@"nodata") certer:poi];
    }
}
+(void)NeedResetNoViewWithView:(UIView*)tab msg:(NSString*)msg andArr:(NSArray*)arr image:(UIImage*)img{
    if (arr.count>0) {
        [tab dismissNoView];
    }else{
        [tab dismissNoView];
        [tab showNoView:msg.length?msg:@"Empty" image:img?:ImageMake(@"nodata") certer:CGPointZero];
    }
}

#pragma mark json格式字符串转字典
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

#pragma mark 字典转json格式字符串
+ (NSString*)dictionaryToJson:(NSDictionary *)dic
{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    NSString *jsonString;
    if (!jsonData) {
        NSLog(@"%@",parseError);
    }else{
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    NSRange range2 = {0,mutStr.length};
    //去掉字符串中的换行符
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    return mutStr;
    
}

#pragma mark 数组转json格式字符串
+ (NSString*)ArrtionaryToJson:(NSMutableArray *)arr
{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&parseError];
    NSString *jsonString;
    if (!jsonData) {
        NSLog(@"%@",parseError);
    }else{
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    NSRange range2 = {0,mutStr.length};
    //去掉字符串中的换行符
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    return mutStr;
    
}
+(UIViewController*)currentViewController{
    //获得当前活动窗口的根视图
    UIViewController* vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (1)
    {
        //根据不同的页面切换方式，逐步取得最上层的viewController
        if ([vc isKindOfClass:[UITabBarController class]]) {
            vc = ((UITabBarController*)vc).selectedViewController;
        }
        if ([vc isKindOfClass:[UINavigationController class]]) {
            vc = ((UINavigationController*)vc).visibleViewController;
        }
        if (vc.presentedViewController) {
            vc = vc.presentedViewController;
        }else{
            break;
        }
    }
    return vc;
}
@end
